package controller

import (
	"gobase/util"
	"net/http"

	"github.com/gin-gonic/gin"
)

type BaseController struct {
}

func (ctl *BaseController) ResponseJSON(ctx *gin.Context, status bool,
	message string, data ...interface{}) {
	ctx.JSON(http.StatusOK, gin.H{
		"status":  status,
		"message": message,
		"data":    data,
	})
}

func (ctl *BaseController) Redirect(ctx *gin.Context, uri string) {
	ctx.Redirect(302, uri)
}

//获取Session Flash信息并清除flash
func (ctl *BaseController) GetSessionFlash(c *gin.Context) []interface{} {
	session := util.GetSession(c)
	flash := session.Flashes()
	session.Save()
	return flash
}

//添加
func (ctl *BaseController) FlashMsg(c *gin.Context, msg string) {
	session := util.GetSession(c)
	session.AddFlash(msg)
	session.Save()
}

//判断提交方式 POST
func (ctl *BaseController) IsPost(httpFunc string) bool {
	if httpFunc == "POST" {
		return true
	}
	return false
}

//判断提交方式 PUT
func (ctl *BaseController) IsPut(httpFunc string) bool {
	if httpFunc == "PUT" {
		return true
	}
	return false
}
